﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class ProjectDAO : IDAO<Project>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(Project entity)
        {
            agrDb.Projects.AddObject(entity);
        }

        public void remove(Project entity)
        {
            agrDb.Projects.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (Project project in agrDb.Projects.Where(s => s.ProjectCode == code))
            {
                agrDb.Projects.DeleteObject(project);
            }
        }

        public void merge(Project entity)
        {
            foreach (Project project in agrDb.Projects.Where(s => s.ProjectCode == entity.ProjectCode))
            {
                project.ProjectContactNumber = entity.ProjectContactNumber;
                project.ProjectContactPerson = entity.ProjectContactPerson;
                project.ProjectExpectedEndDate = entity.ProjectExpectedEndDate;
                project.ProjectLocation = entity.ProjectLocation;
                project.ProjectName = entity.ProjectName;
                project.ProjectStartDate = entity.ProjectStartDate;
                project.ProjectType = entity.ProjectType;
            }

            //Select ALL
            //var students = (from s in agrDb.Students
            //                where s.StudentCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var students2 = (from s in agrDb.Students
            //                select new
            //                {
            //                    s.StudentCourses,
            //                    s.StudentDisability
            //                });

            //Order rows
            //var orderedListByCourses = students.OrderBy(s => s.StudentCourses);

        }

        public Project findById(string code)
        {
            Project project = new Project();

            foreach (Project proj in agrDb.Projects.Where(s => s.ProjectCode == code))
            {
                project = proj;
            }

            return project;
        }

        public IEnumerable<Project> findAll()
        {
            var projects = (from proj in agrDb.Projects
                            select proj);

            return projects;
        }

        public IEnumerable<Project> findInRange(int firstResult, int maxResuslts)
        {
            var projects = (from proj in agrDb.Projects
                            select proj);

            return projects.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var projects = (from proj in agrDb.Projects
                            select proj).ToList();

            return projects.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}